home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / HUGS1 / hs / examples < prev    next >
Text File  |  1995-02-14  |  3KB  |  82 lines

  1. -- Some examples of functional programming for Hugs
  2.  
  3. -- Factorials:
  4.  
  5. fact n = product [1..n]                     -- a simple definition
  6.  
  7. fac n  = if n==0 then 1 else n * fac (n-1)  -- a recursive definition
  8.  
  9. fac' 0 = 1                                  -- using two equations
  10. fac' n = n * fac (n-1)
  11.  
  12. facts, facts' :: (Enum a, Num a) => [a]
  13. facts          = scanl (*) 1 [1..]            -- infinite list of factorials
  14. facts'         = 1 : zipWith (*) facts' [1..] -- another way of doing it
  15.  
  16. facFix :: Num a => a -> a
  17. facFix = fixedPt f                          -- using a fixed point combinator
  18.          where  f g 0       = 1             -- overlapping patterns
  19.                 f g n       = n * g (n-1)
  20.                 fixedPt f = g where g = f g -- fixed point combinator
  21.  
  22. facCase :: Integral a => a -> a
  23. facCase  = \n -> case n of
  24.                    0     ->  1
  25.                    (m+1) -> (m+1) * facCase m
  26.  
  27. -- Fibonacci numbers:
  28.  
  29. fib 0     = 0                               -- using pattern matching:
  30. fib 1     = 1                               -- base cases...
  31. fib (n+2) = fib n + fib (n+1)               -- recursive case
  32.  
  33. fastFib n    = fibs !! n                    -- using an infinite stream
  34.                where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
  35.  
  36. -- Perfect numbers:
  37.  
  38. factors n    = [ i | i<-[1..n-1], n `mod` i == 0 ]
  39. perfect n    = sum (factors n) == n
  40. firstperfect = head perfects
  41. perfects     = filter perfect [1::Int..]
  42.  
  43. -- Prime numbers:
  44.  
  45. primes      :: Integral a => [a]
  46. primes       = map head (iterate sieve [2..])
  47. sieve (p:xs) = [ x | x<-xs, x `rem` p /= 0 ]
  48.  
  49. -- Pythagorean triads:
  50.  
  51. triads n     = [ (x,y,z) | let ns=[1..n], x<-ns, y<-ns, z<-ns, x*x+y*y==z*z ]
  52.  
  53. -- The Hamming problem:
  54.  
  55. hamming     :: [Int]
  56. hamming      = 1 : (map (2*) hamming || map (3*) hamming || map (5*) hamming)
  57.                where (x:xs) || (y:ys)  | x==y  =  x : (xs || ys)
  58.                                        | x<y   =  x : (xs || (y:ys))
  59.                                        | y<x   =  y : (ys || (x:xs))
  60.  
  61. -- Digits of e:
  62.  
  63. eFactBase ::  [Int]
  64. eFactBase  =  map head (iterate scale (2:repeat 1))
  65.  
  66. scale     ::  Integral a => [a] -> [a]
  67. scale      =  renorm . map (10*) . tail
  68. renorm ds  =  foldr step [0] (zip ds [2..])
  69.  
  70. step (d,n) bs | (d `mod` n + 9) < n  = (d `div` n) : b : tail bs
  71.               | otherwise            = c           : b : tail bs
  72.               where b' = head bs
  73.                     b  = (d+b') `mod` n
  74.                     c  = (d+b') `div` n
  75.  
  76. -- Pascal's triangle
  77.  
  78. pascal :: [[Int]]
  79. pascal  = iterate (\row -> zipWith (+) ([0]++row) (row++[0])) [1]
  80.  
  81. showPascal = (layn . map show . take 14) pascal
  82.